<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品id" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入商品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="轮播图" prop="images">
        <el-input
          v-model="queryParams.images"
          placeholder="请输入轮播图"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简介" prop="info">
        <el-input
          v-model="queryParams.info"
          placeholder="请输入简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成本" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原价" prop="otPrice">
        <el-input
          v-model="queryParams.otPrice"
          placeholder="请输入原价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返多少积分" prop="giveIntegral">
        <el-input
          v-model="queryParams.giveIntegral"
          placeholder="请输入返多少积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input
          v-model="queryParams.stock"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销量" prop="sales">
        <el-input
          v-model="queryParams.sales"
          placeholder="请输入销量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单位名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮费" prop="postage">
        <el-input
          v-model="queryParams.postage"
          placeholder="请输入邮费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="stopTime">
        <el-date-picker clearable
          v-model="queryParams.stopTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否包邮" prop="isPostage">
        <el-input
          v-model="queryParams.isPostage"
          placeholder="请输入是否包邮"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除 0未删除1已删除" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入删除 0未删除1已删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当天参与活动次数" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入当天参与活动次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示" prop="isShow">
        <el-input
          v-model="queryParams.isShow"
          placeholder="请输入显示"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间段ID" prop="timeId">
        <el-input
          v-model="queryParams.timeId"
          placeholder="请输入时间段ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运费模板ID" prop="tempId">
        <el-input
          v-model="queryParams.tempId"
          placeholder="请输入运费模板ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体积" prop="volume">
        <el-input
          v-model="queryParams.volume"
          placeholder="请输入体积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限购总数,随减" prop="quota">
        <el-input
          v-model="queryParams.quota"
          placeholder="请输入限购总数,随减"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限购总数显示.不变" prop="quotaShow">
        <el-input
          v-model="queryParams.quotaShow"
          placeholder="请输入限购总数显示.不变"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:storeSeckill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:storeSeckill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:storeSeckill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:storeSeckill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeSeckillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品秒杀产品表id" align="center" prop="id" v-if="true"/>
      <el-table-column label="商品id" align="center" prop="productId" />
      <el-table-column label="推荐图" align="center" prop="image" width="100">
          <template slot-scope="scope">
              <image-preview :src="scope.row.image" :width="50" :height="50"/>
          </template>
      </el-table-column>
      <el-table-column label="轮播图" align="center" prop="images" />
      <el-table-column label="活动标题" align="center" prop="title" />
      <el-table-column label="简介" align="center" prop="info" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="成本" align="center" prop="cost" />
      <el-table-column label="原价" align="center" prop="otPrice" />
      <el-table-column label="返多少积分" align="center" prop="giveIntegral" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="销量" align="center" prop="sales" />
      <el-table-column label="单位名" align="center" prop="unitName" />
      <el-table-column label="邮费" align="center" prop="postage" />
      <el-table-column label="内容" align="center" prop="description" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="秒杀状态 0=关闭 1=开启" align="center" prop="status" />
      <el-table-column label="是否包邮" align="center" prop="isPostage" />
      <el-table-column label="删除 0未删除1已删除" align="center" prop="isDel" />
      <el-table-column label="当天参与活动次数" align="center" prop="num" />
      <el-table-column label="显示" align="center" prop="isShow" />
      <el-table-column label="时间段ID" align="center" prop="timeId" />
      <el-table-column label="运费模板ID" align="center" prop="tempId" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="体积" align="center" prop="volume" />
      <el-table-column label="限购总数,随减" align="center" prop="quota" />
      <el-table-column label="限购总数显示.不变" align="center" prop="quotaShow" />
      <el-table-column label="规格 0=单 1=多" align="center" prop="specType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:storeSeckill:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:storeSeckill:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品秒杀产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="推荐图">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="轮播图" prop="images">
          <el-input v-model="form.images" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="简介" prop="info">
          <el-input v-model="form.info" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="成本" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入成本" />
        </el-form-item>
        <el-form-item label="原价" prop="otPrice">
          <el-input v-model="form.otPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="返多少积分" prop="giveIntegral">
          <el-input v-model="form.giveIntegral" placeholder="请输入返多少积分" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="销量" prop="sales">
          <el-input v-model="form.sales" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="单位名" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名" />
        </el-form-item>
        <el-form-item label="邮费" prop="postage">
          <el-input v-model="form.postage" placeholder="请输入邮费" />
        </el-form-item>
        <el-form-item label="内容" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="stopTime">
          <el-date-picker clearable
            v-model="form.stopTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否包邮" prop="isPostage">
          <el-input v-model="form.isPostage" placeholder="请输入是否包邮" />
        </el-form-item>
        <el-form-item label="删除 0未删除1已删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除 0未删除1已删除" />
        </el-form-item>
        <el-form-item label="当天参与活动次数" prop="num">
          <el-input v-model="form.num" placeholder="请输入当天参与活动次数" />
        </el-form-item>
        <el-form-item label="显示" prop="isShow">
          <el-input v-model="form.isShow" placeholder="请输入显示" />
        </el-form-item>
        <el-form-item label="时间段ID" prop="timeId">
          <el-input v-model="form.timeId" placeholder="请输入时间段ID" />
        </el-form-item>
        <el-form-item label="运费模板ID" prop="tempId">
          <el-input v-model="form.tempId" placeholder="请输入运费模板ID" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入体积" />
        </el-form-item>
        <el-form-item label="限购总数,随减" prop="quota">
          <el-input v-model="form.quota" placeholder="请输入限购总数,随减" />
        </el-form-item>
        <el-form-item label="限购总数显示.不变" prop="quotaShow">
          <el-input v-model="form.quotaShow" placeholder="请输入限购总数显示.不变" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStoreSeckill, getStoreSeckill, delStoreSeckill, addStoreSeckill, updateStoreSeckill } from "@/api/system/storeSeckill";

export default {
  name: "StoreSeckill",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品秒杀产品表格数据
      storeSeckillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: undefined,
        image: undefined,
        images: undefined,
        title: undefined,
        info: undefined,
        price: undefined,
        cost: undefined,
        otPrice: undefined,
        giveIntegral: undefined,
        sort: undefined,
        stock: undefined,
        sales: undefined,
        unitName: undefined,
        postage: undefined,
        description: undefined,
        startTime: undefined,
        stopTime: undefined,
        status: undefined,
        isPostage: undefined,
        isDel: undefined,
        num: undefined,
        isShow: undefined,
        timeId: undefined,
        tempId: undefined,
        weight: undefined,
        volume: undefined,
        quota: undefined,
        quotaShow: undefined,
        specType: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "商品秒杀产品表id不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "推荐图不能为空", trigger: "blur" }
        ],
        images: [
          { required: true, message: "轮播图不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "活动标题不能为空", trigger: "blur" }
        ],
        info: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        cost: [
          { required: true, message: "成本不能为空", trigger: "blur" }
        ],
        otPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        giveIntegral: [
          { required: true, message: "返多少积分不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        stock: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        sales: [
          { required: true, message: "销量不能为空", trigger: "blur" }
        ],
        unitName: [
          { required: true, message: "单位名不能为空", trigger: "blur" }
        ],
        postage: [
          { required: true, message: "邮费不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        stopTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "添加时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "秒杀状态 0=关闭 1=开启不能为空", trigger: "blur" }
        ],
        isPostage: [
          { required: true, message: "是否包邮不能为空", trigger: "blur" }
        ],
        isDel: [
          { required: true, message: "删除 0未删除1已删除不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "当天参与活动次数不能为空", trigger: "blur" }
        ],
        isShow: [
          { required: true, message: "显示不能为空", trigger: "blur" }
        ],
        timeId: [
          { required: true, message: "时间段ID不能为空", trigger: "blur" }
        ],
        tempId: [
          { required: true, message: "运费模板ID不能为空", trigger: "blur" }
        ],
        weight: [
          { required: true, message: "重量不能为空", trigger: "blur" }
        ],
        volume: [
          { required: true, message: "体积不能为空", trigger: "blur" }
        ],
        quota: [
          { required: true, message: "限购总数,随减不能为空", trigger: "blur" }
        ],
        quotaShow: [
          { required: true, message: "限购总数显示.不变不能为空", trigger: "blur" }
        ],
        specType: [
          { required: true, message: "规格 0=单 1=多不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品秒杀产品列表 */
    getList() {
      this.loading = true;
      listStoreSeckill(this.queryParams).then(response => {
        this.storeSeckillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productId: undefined,
        image: undefined,
        images: undefined,
        title: undefined,
        info: undefined,
        price: undefined,
        cost: undefined,
        otPrice: undefined,
        giveIntegral: undefined,
        sort: undefined,
        stock: undefined,
        sales: undefined,
        unitName: undefined,
        postage: undefined,
        description: undefined,
        startTime: undefined,
        stopTime: undefined,
        createTime: undefined,
        status: 0,
        isPostage: undefined,
        isDel: undefined,
        num: undefined,
        isShow: undefined,
        timeId: undefined,
        tempId: undefined,
        weight: undefined,
        volume: undefined,
        quota: undefined,
        quotaShow: undefined,
        specType: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品秒杀产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getStoreSeckill(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改商品秒杀产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateStoreSeckill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStoreSeckill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品秒杀产品编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delStoreSeckill(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/storeSeckill/export', {
        ...this.queryParams
      }, `storeSeckill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
